package com.opennews.openplatform

import com.opennews.openplatform.security.UserActiveTokenManager
import groovy.transform.CompileStatic

@CompileStatic
class BootStrap {
    AppReleaseLogService appReleaseLogService
    BankService bankService
    RegionService regionService
    IncomeExpenseDetailBaseTypeService incomeExpenseDetailBaseTypeService
    RoleService roleService
    RequestMapService requestMapService
    UserActiveTokenManager userActiveTokenManager

    def init = { servletContext ->
        // For data initialization.
        appReleaseLogService.init()
        bankService.init()
        incomeExpenseDetailBaseTypeService.init()
        regionService.init()
        requestMapService.init()
        roleService.init()

        // To make the setting work we have to clear the cache.
        // Just in case any error causes requestMap failed. CLears the cache once system startup.
        // For example, the Requestmap.httpMethod value must be uppercase. Lowercase value would cause clearCachedRequestmaps failed.
        // Once clearCachedRequestmaps failed, any access will be blocked.
        // BE CAUTION!!!!
        //
        // IMPORTANT!!!
        // If you see the error of "No enum constant org.springframework.http.HttpMethod." when you are trying to run project, go and
        // check the Requestmap.httpMethod values in database table.
        // This field only accepts the string value of http methods. Any empty string of other string values will cause this error.
        requestMapService.refreshCache()

        userActiveTokenManager.reloadCachedUserActiveTokenRecords()
    }

    def destroy = { }
}